{extend name="sitehome@style/base"/}
{block name="resources"}
<style>
    /*
    .layui-tab .layui-tab-title li {
        border: 1px solid #e5e5e5;
        border-right: none;
        height: 36px;
        line-height: 36px;
    }
    .li {
        background: #12B7F5 !important;
    }
    .li-a{
        color: #fff !important;
    }

    a { color:#333; transition:0.5s; }
    a:hover { color:#333; }
    a:active {color: #333; !*鼠标按下的颜色变化*!}
    a:link {
        color: #333; !*连接默认的颜色变化*!
    }
    a:visited {
        color: #333; !*连接访问的颜色变化*!
    }
    */
    .is-top{
        color: red;
    }
</style>
{/block}
{block name="main"}

<div class="layui-tab" style="margin:0px 20px;">
    <button  class="layui-btn {if $p_category_id != 0 }layui-btn-primary{/if}" onclick="location.href = nc.url('Sns://sitehome/info/infolist')">全部</button>
    {foreach $category_data as $key=>$vo }
        <button style="margin:0px 0px ;" class="layui-btn {if $p_category_id != $vo.category_id }layui-btn-primary{/if}" onclick="location.href = nc.url('Sns://sitehome/info/infolist',{'p_category_id':'{$vo.category_id}'})" >{$vo.name}</button>
    {/foreach}

    <!--
    <ul class="layui-tab-title">
        <li {if $p_category_id == 0 } class="li" {/if}>
            <a href="{:url('sns/sitehome/info/infolist')}" {if $p_category_id == 0 } class="li-a" {/if}>全部</a>
        </li>
        {foreach $category_data as $key=>$vo }
            <li {if $vo.category_id == $p_category_id} class="li" {/if}>
                <button class="layui-btn" href="{:url('sns/sitehome/info/infolist?p_category_id=')}{$vo.category_id}" {if $p_category_id == $vo.category_id } class="li-a" {/if}>{$vo.name}</a>
            </li>
        {/foreach}
    </ul>
    -->
    <div class="layui-tab-content">
    </div>
</div>

<div class="nc-function-search">
    <button class="layui-btn" onclick="location.href = nc.url('Sns://sitehome/info/addInfo')">添加</button>
    <div class="layui-form">
        <div class="layui-input-inline nc-len-mid">
            <!--<select  name="p_category_id"  lay-filter="p_category_id" lay-verify="p_category_id">
                <option value="">请选择分类</option>
                {foreach $category as $key=>$vo }
                <option value="{$vo.category_id}" >{$vo.name}</option>
                {/foreach}
            </select>-->
        </div>
        <div class="layui-input-inline nc-len-mid">
            <select  name="category_id" lay-filter="console">
                <option value="">请选择分类</option>
                {foreach $category as $key=>$vo }
                <option value="{$vo.category_id}" >{$vo.name}</option>
                {/foreach}
            </select>
        </div>
        <div class="layui-input-inline nc-len-mid nc-search-btn">
            <input type="text" id="search" name="search" placeholder="标题" autocomplete="off" class="layui-input">
        </div>

        <button class="layui-btn-primary layui-btn" lay-filter="search" lay-submit>筛选</button>
    </div>
</div>

<div class="nc-table-box">
    <table id="info_list" lay-filter="article" class="layui-table"></table>
</div>

<script type="text/html" id="num_info">
    <span>{{d.click}}&nbsp;/&nbsp;{{d.comment_count}}</span>
</script>

<script type="text/html" id="time_info">
    <span>{{ nc.time_to_date(d.create_time) }}</span>
</script>

<!--分类
<script type="text/html" id="category_name">
<span>{{d.category_name}}</span>
<a href="javascript:;" class="default" lay-event="category_name">二维码</a>
</script>
-->

<script type="text/html" id="title">
    <ul>
        <li>
            <a target="_blank" onclick="window.open( nc.url('Sns://wap/category/info',{info_id:'{{d.info_id}}'}))">{{d.title}}</a>
        </li>
        <li>
            <span>{{d.category_name}}&nbsp;&nbsp;</span>

            {{# if(d.uid > 0){ }}
            <span class="is-top">自&nbsp;&nbsp;</span>
            {{# } }}

            {{# if(d.is_top == 1){ }}
                <span class="is-top">顶&nbsp;&nbsp;&nbsp;</span>
            {{# } }}

            <a href="javascript:;" lay-event="category_name">
                <img src="SNS_IMG/er_wei_ma.png" style="width:13px" alt="扫码查看详情">
                <input type="hidden" name="qr_code" value="{{d.qrcode}}">
            </a>

            <!--<a href="javascript:;" class="layui-icon layui-icon-app" lay-event="category_name"></a>-->
        </li>
    </ul>
</script>

<!--排序-->
<script type="text/html" id="sort">
    <input type="text" value="{{d.sort}}"  class="layui-input" onchange="infoSort({{d.info_id}}, this)"/>
</script>

<script type="text/html" id="operation">
    <a class="default" lay-event="isTop">
        {{# if(d.is_top == 1){ }}
        取消置顶
        {{# }else{ }}
        置顶
        {{# } }}
    </a>
    {{# if(d.state == 0){ }}
        <a class="default" lay-event="state">审核</a>
    {{# } }}

    <a class="default" lay-event="edit">编辑</a>
    <a class="default" lay-event="del">删除</a>
</script>

{/block}
{block name="script"}
<script type="text/javascript">
    var page_index =  '{$page_index}';
    var console =  '{$console}';
    var p_category_id =  '{$p_category_id}';
    var form;
    //排序更新
    function infoSort(info_id,_this){
        var sort = $(_this).val();

        $.ajax({
            type: "post",
            url: nc.url("sns://sitehome/info/updateInfo"),
            data: {
                'info_id':info_id,
                'sort': sort,
            },
            dataType: "JSON",
            success: function (res) {
            }
        });
    };

    layui.use(['form','layedit', 'util', 'laytpl'], function () {
        form = layui.form;
        //搜索submit提交
        form.on('submit(search)', function(data){
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
        });

        //获取二级分类
        form.on('select(p_category_id)', function (obj) {

            var p_category_id = $("select[name='p_category_id']").val();

            $.ajax({
                type: "post",
                url: nc.url("sns://sitehome/info/getCategory"),
                data: {
                    'category_id': p_category_id,
                },
                dataType: "JSON",
                success: function (res) {

                    var arrData = JSON.parse(res);

                    $("select[name=category_id] option:gt(0)").remove();

                    var html = '';
                    for(var i = 0; i < arrData.length; i++){
                        html += '<option value="'+arrData[i]['category_id']+'" >'+arrData[i]['name']+'</option>';
                    }

                    $("select[name=category_id]").append(html);
                    form.render();
                }
            });
        });
    });

    var table = new Table({
        elem: '#info_list',
        filter : "article",
        url : nc.url("sns://sitehome/info/infoList",{'p_category_id':p_category_id}),
        page:{
            layout: ['count', 'limit', 'prev', 'page', 'next'],
            limit:10,
            curr:page_index
        },
        cols: [
            [
            {
                type: 'checkbox',
                unresize : 'true'
            },{
                field: 'title',
                width: '25%',
                title: '信息',
                align: 'left',
                toolbar: '#title',
            },{
                field: 'state_as',
                width: '8%',
                title: '状态',
                align: 'center',
                unresize : 'true'
            },{
                field: 'release_name',
                width: '10%',
                title: '发布人',
                align: 'center',
                unresize : 'true'
            },{

                width: '10%',
                title: '排序',
                align: 'center',
                toolbar: '#sort'
            },{
                width: '10%',
                title: '浏览/收藏',
                align: 'center',
                templet: function (data) {
                    return '<span>'+data.visit+'/'+data.collection+'</span>';
                }
            },{
                field: 'add_time',
                width: '25%',
                title: '发布/刷新时间',
                align: 'center',
                templet: function (data) {
                    return '<span>发布：'+data.add_time+'</span><span>刷新：'+data.reflash+'</span>';
                }
            },{
                title: '操作',
                width: '10%',
                toolbar: '#operation',
                align: 'center',
                unresize : 'true'
            }]
        ]
    });

    //监听工具条
    table.tool(function(obj){
        var info_id = obj.data.info_id;
        var is_top = obj.data.is_top;
        var qa_code = obj.data.qrcode;
        var page_index = $('.layui-laypage-em').next().html();

        if(is_top == 0){
            is_top =2;
        }

        switch (obj.event) {
            case "edit":
                window.location.href = nc.url('sns://sitehome/info/editInfo', {"info_id": info_id,page: page_index});
                break;
            case "del":
                deleteArticle(info_id);
                break;
            case "state":
                stateArticle(info_id);
                break;
            case "isTop":
                isTop(info_id,is_top);
                break;
            case "category_name":
                // var data = '{"addon_name":"Sns","h5_url":"sns://wap/category/lists","name":"SNS_CATEGORY","title":"分类"}';
                getPromote(qa_code,info_id);
                break;
        }
    });

    //删除
    function deleteArticle(info_id){

        layer.confirm('确定删除吗?', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                type: "post",
                url: nc.url("sns://sitehome/info/deleteInfo"),
                data: {
                    'info_id': info_id,
                },
                dataType: "JSON",
                success: function (res) {
                    layer.msg(res.message);
                    location.reload();
                }
            });
        }, function () {
            layer.close();
        });
    }

    //审核
    function stateArticle(info_id){

        layer.confirm('请审核当前信息是否符合上线要求。', {
            btn: ['通过', '拒绝'] //可以无限个按钮
        }, function(index, layero){
            stateAjax(info_id,1);
            //按钮【通过】的回调
        }, function(index){
            stateAjax(info_id,2);
            //按钮【拒绝】的回调
        });
    }

    function  stateAjax(info_id,state) {
        $.ajax({
            type: "post",
            url: nc.url("sns://sitehome/info/updateInfo"),
            data: {
                'info_id':info_id,
                'state': state,
            },
            dataType: "JSON",
            success: function (res) {

                layer.msg(res.message);
                location.reload();
            }
        });
    }

    //是否置顶
    function isTop(info_id,is_top) {
        $.ajax({
            type: "post",
            url: nc.url("sns://sitehome/info/updateInfo"),
            data: {
                'info_id':info_id,
                'is_top': is_top,
            },
            dataType: "JSON",
            success: function (res) {

                layer.msg(res.message);
                location.reload();
            }
        });
    }

    var show_promote_flag = true;
    var index = 0;

    //二维码调用
    function getPromote(qr_code,info_id) {

        var url = nc.url("sns://sitehome/info/promote");
        if (show_promote_flag) {
            show_promote_flag = false;
            $.post(url, {qr_code: qr_code,info_id:info_id}, function (str) {
                index = layer.open({
                    type: 1,
                    title: "扫码查看详情",
                    content: str,
                    btn: [],
                    area: ['500px', '400px'], //宽高
                    maxWidth: 1920,
                    cancel: function (index, layero) {
                        show_promote_flag = true;
                    },
                    end: function () {
                        show_promote_flag = true;
                    }
                });
            });
        }
    }
</script>
{/block}